//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using DNAide.Core;

namespace DNAide.Drawing
{
	public static partial class BitmapAide
	{
		internal static int CREATE_TEXT_BITMAP_DEFAULT_MAX_HEIGHT = 1000;
		internal static int CREATE_TEXT_BITMAP_DEFAULT_MAX_WIDTH = 1000;

		/// <summary>
		/// Draws a text onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		public static void DrawTextOnToCanvasBitmap(ref Bitmap canvasBitmap, String text, Font font, Color foreColor)
		{
			DrawTextOnToCanvasBitmap(ref canvasBitmap, text, font, foreColor, null, null);
		}


		/// <summary>
		/// Draws a text onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		/// <param name="stringFormat"></param>
		public static void DrawTextOnToCanvasBitmap(ref Bitmap canvasBitmap, String text, Font font, Color foreColor, StringFormat stringFormat)
		{
			DrawTextOnToCanvasBitmap(ref canvasBitmap, text, font, foreColor, stringFormat, null);
		}

		
		/// <summary>
		/// Draws a text onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		/// <param name="stringFormat"></param>
		/// <param name="graphicsSettings"></param>
		public static void DrawTextOnToCanvasBitmap(ref Bitmap canvasBitmap, String text, Font font, Color foreColor, StringFormat stringFormat, GraphicsSettings graphicsSettings)
		{
			DrawTextOnToCanvasBitmap(ref canvasBitmap, text, font, foreColor, stringFormat, graphicsSettings, new Rectangle(0, 0, canvasBitmap.Width, canvasBitmap.Height));
		}

		
		/// <summary>
		/// Draws a text onto a canvas bitmap
		/// </summary>
		/// <param name="canvasBitmap"></param>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		/// <param name="stringFormat"></param>
		/// <param name="graphicsSettings"></param>
		/// <param name="rectangle"></param>
		public static void DrawTextOnToCanvasBitmap(ref Bitmap canvasBitmap, String text, Font font, Color foreColor, StringFormat stringFormat, GraphicsSettings graphicsSettings, Rectangle rectangle)
		{
			Bitmap mergedBitmap = new Bitmap(canvasBitmap.Width, canvasBitmap.Height);
			Brush foreBrush = new SolidBrush(foreColor);
			Graphics graphics = Graphics.FromImage(mergedBitmap);
			if (graphicsSettings != null)
			{
				graphicsSettings.ApplySettings(graphics);
			}
			graphics.DrawImage(canvasBitmap, 0, 0, canvasBitmap.Width, canvasBitmap.Height);
			graphics.DrawString(text, font, foreBrush, rectangle, stringFormat);
			graphics.Dispose();
			canvasBitmap.Dispose(); canvasBitmap = null;
			canvasBitmap = BitmapAide.CopyBitmap(mergedBitmap);
			mergedBitmap.Dispose(); mergedBitmap = null;
		}


		/// <summary>
		/// Creates a simple text based image
		/// </summary>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		/// <param name="backColor"></param>
		/// <returns>Bitmap</returns>
		public static Bitmap CreateTextBitmap(String text, Font font, Color foreColor, Color backColor)
		{
			return CreateTextBitmap(text, font, foreColor, backColor, CREATE_TEXT_BITMAP_DEFAULT_MAX_WIDTH, CREATE_TEXT_BITMAP_DEFAULT_MAX_HEIGHT, null, true, null);
		}


		/// <summary>
		/// Creates a simple text based image
		/// </summary>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		/// <param name="backColor"></param>
		/// <param name="maxWidth"></param>
		/// <param name="maxHeight"></param>
		/// <param name="stringFormat"></param>
		/// <param name="clipToText"></param>
		/// <param name="graphicsSettings"></param>
		/// <returns></returns>
		public static Bitmap CreateTextBitmap(String text, Font font, Color foreColor, Color backColor, Int32 maxWidth, Int32 maxHeight, StringFormat stringFormat, Boolean clipToText, GraphicsSettings graphicsSettings)
		{
			return CreateTextBitmap(text, font, foreColor, backColor, maxWidth, maxHeight, stringFormat, clipToText, graphicsSettings, new Point(0,0));
		}


		/// <summary>
		/// Creates a simple text based image
		/// </summary>
		/// <param name="text"></param>
		/// <param name="font"></param>
		/// <param name="foreColor"></param>
		/// <param name="backColor"></param>
		/// <param name="maxWidth"></param>
		/// <param name="maxHeight"></param>
		/// <param name="stringFormat"></param>
		/// <param name="clipToText"></param>
		/// <param name="graphicsSettings"></param>
		/// <param name="drawPoint"></param>
		/// <returns>Bitmap</returns>
		public static Bitmap CreateTextBitmap(String text, Font font, Color foreColor, Color backColor, Int32 maxWidth, Int32 maxHeight, StringFormat stringFormat, Boolean clipToText, GraphicsSettings graphicsSettings, Point drawPoint)
		{
			Bitmap textBitmap = new Bitmap(maxWidth, maxHeight, PixelFormat.Format32bppArgb);
			Graphics graphics = Graphics.FromImage(textBitmap);
			if (graphicsSettings != null)
			{
				graphicsSettings.ApplySettings(graphics);
			}

			// Backfill
			Brush backBrush = new SolidBrush(backColor);
			graphics.FillRectangle(backBrush, 0, 0, textBitmap.Width, textBitmap.Height);

			int newWidth = textBitmap.Width;
			int newHeight = textBitmap.Height;
			StringFormat sf = new StringFormat();
			if (clipToText)
			{
				// Measure bounding rectangle
				SizeF szf = graphics.MeasureString(text, font, maxWidth, stringFormat);
				newWidth = CastAide.AsInt32(szf.Width + 1);
				newHeight = CastAide.AsInt32(szf.Height + 1);
				if (newWidth > maxWidth)
				{
					newWidth = maxWidth;
				}
				if (newHeight > maxHeight)
				{
					newHeight = maxHeight;
				}
			}

			Rectangle rect = new Rectangle(drawPoint.X, drawPoint.Y, newWidth, newHeight);

			// Set the font brush and bounds
			Brush foreBrush = new SolidBrush(foreColor);

			// Draw the string
			graphics.DrawString(text, font, foreBrush, rect, stringFormat);

			graphics.Dispose();

			if (clipToText)
			{
				// Clip to proper height
				newWidth += drawPoint.X;
				newHeight += drawPoint.Y;
				if (newWidth > maxWidth || newWidth < 1)
				{
					newWidth = maxWidth;
				}
				if (newHeight > maxHeight || newHeight < 1)
				{
					newHeight = maxHeight;
				}
				Rectangle clipRect = new Rectangle(0, 0, newWidth, newHeight);
				Bitmap textBitmapClipped = textBitmap.Clone(clipRect, PixelFormat.Format32bppArgb);
				return textBitmapClipped;
			}
			return textBitmap;

		}

	}
}
